package com.atyixiang.hosp.Controller;

import com.atyixiang.model.hosp.HospitalSet;
import com.atyixiang.hosp.Service.HospitalSetService;
import com.atyixiang.cmn.utils.MD5;
import com.atyixiang.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.atyixiang.commom.result.Result;
import io.swagger.annotations.Api;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理") //swagger2测试中的 中文提示
@RestController
@RequestMapping("/admin/Hosp")
//@CrossOrigin
public class HospitalSetController {

    //注入Service
    @Autowired
    private HospitalSetService hospService;

    @ApiOperation(value = "查询医院设置信息") //swagger2测试中的 方法中文提示
    @PostMapping("/show")
    public Result show(){
        /*查询信息 返回给统一返回结果*/
        List<HospitalSet> list=hospService.list();
        return Result.ok(list);
    }


    //条件分页查询
    @ApiOperation(value = "条件分页查询医院设置信息") //swagger2测试中的 方法中文
    @PostMapping(value = "/showAll/{current}/{limit}")
    public Result showAll(
            @PathVariable   Integer current,
            @PathVariable   Integer limit,
            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){

        //创建page对象 分页
        Page<HospitalSet> page = new Page<>(current,limit);
       //构造条件
        QueryWrapper<HospitalSet> wrapper =new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();//医院编号
        /*判断添加 是否为空*/
        if (!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)){
            wrapper.like("hoscode",hospitalSetQueryVo.getHoscode());
        }

        //调用方法实现分页查询
        Page<HospitalSet> rolePage = hospService.page(page,wrapper);
        return Result.ok(rolePage);
    }

    //添加角色信息
    @ApiOperation(value = "添加医院设置信息") //swagger2测试中的 方法中文
    @PostMapping(value = "/AddHospInfo")
    public Result AddRoleInfo(@RequestBody HospitalSet hospitalSet){
        //设置状态1使用 0不能使用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));

        /*三元运算符 判断是否 添加成功*/
        return hospService.save(hospitalSet)?Result.ok():Result.fail();
    }

    //按id查询角色信息
    @ApiOperation(value = "按id查询医院设置信息") //swagger2测试中的 方法中文
    @GetMapping(value = "/HospInfoById/{id}")
    public Result RoleInfoById(@PathVariable Integer id){
        /*按id查询信息*/
        HospitalSet serviceById = hospService.getById(id);
        return Result.ok(serviceById);
    }

    //修改角色信息
    @ApiOperation(value = "修改医院设置信息") //swagger2测试中的 方法中文
    @PostMapping(value = "/UpdateHospInfo")
    public Result UpdateRoleInfo(@RequestBody HospitalSet hospitalSet ){
        /*三元运算符 判断是否 修改成功*/
        return hospService.updateById(hospitalSet)?Result.ok():Result.fail();
    }

    //删除角色信息
    @ApiOperation(value = "删除医院设置信息") //swagger2测试中的 方法中文
    @PostMapping(value = "/DelHospInfo/{id}")
    public Result DelRoleInfo(@PathVariable Integer id ){
        /*三元运算符 判断是否 删除成功*/
        return hospService.removeById(id)?Result.ok():Result.fail();
    }

    //批删角色信息
    @ApiOperation(value = "批删医院设置信息") //swagger2测试中的 方法中文
    @PostMapping(value = "/DelsHospInfo")
    public Result DelsRoleInfo(@RequestBody List<Integer> ids ){
        /*三元运算符 判断是否 删除成功*/
        return hospService.removeByIds(ids)?Result.ok():Result.fail();
    }

    //医院设置锁定和解锁
    @ApiOperation(value = "锁定") //swagger2测试中的 方法中文
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        return hospService.updateById(hospitalSet)?Result.ok():Result.fail();
    }

    //发送签名密钥
    @ApiOperation(value = "发送密钥") //swagger2测试中的 方法中文
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }
}
